<?php
/**
 * SFKNews
 * 私房库 www.sifangku.com 教学项目
 *
 * @desc 新闻管理
 * @copyright 私房库 www.sifangku.com 保留所有版权
 * @author 孙胜利 www.sunshengli.com
 * @version 1.01
 *
 **/
namespace Admin\Controller;
use Common\Controller;
use Common\Tree;
use Admin\Model\TypeModel;
use Admin\Model\ArticleModel;
use Common\Page;
if(!defined('SITE')) exit('Access Denied');
class ArticleController extends Controller {
	public function __construct(){
		$this->model=new ArticleModel();
		parent::__construct();
	}
	public function index(){
		$page=new Page($this->model->count(array('isdelete=?',array('isdelete'=>0))),array('url'=>'/News/admin.php/article/index/{getIndex}/{pageNum}'));
		$this->view->setData('article',$this->model->selectByPage($page->getLimit()));
		$this->view->setData('page',$page->getHtml());
		$this->view->display();
	}
	public function add(){
		if(!empty($_POST)){
			if($this->model->add($_POST)){
				if(isAjaxRequest()){
					exit('{"status":1,"message":"添加成功."}');
				}
				$this->skip(array(
						'status'=>'success',
						'url'=>'/News/admin.php/Article/index',
						'message'=>'添加成功.'
				));
			}else{
				if(isAjaxRequest()){
					exit('{"status":0,"message":"'.$this->model->getError().'"}');
				}
				$this->skip(array(
						'status'=>'danger',
						'url'=>'/News/admin.php/Article/add',
						'message'=>$this->model->getError()
				));
			}
		}
		
		$typeModel=new TypeModel();
		$tree=new Tree($typeModel->getAll());
		$this->view->setData('arctype',$tree->getAll());
		$this->view->display();
	}
	public function update(){
		$data=$this->model->select(get('id'));
		if($data===false || count($data)!=1){
			$this->skip(array(
					'status'=>'danger',
					'url'=>'/News/admin.php/article/index',
					'message'=>'新闻不存在.'
			));
		}
		if(!empty($_POST)){
			if($this->model->update($_POST,get('id'))!==false){
				if(isAjaxRequest()){
					exit('{"status":1,"message":"修改成功."}');
				}
				$this->skip(array(
						'status'=>'success',
						'url'=>'/News/admin.php/article/index',
						'message'=>'修改成功.'
				));
			}else{
				if(isAjaxRequest()){
					exit('{"status":0,"message":"'.$this->model->getError().'"}');
				}
				$this->skip(array(
						'status'=>'danger',
						'url'=>'/News/admin.php/article/update/id/'.get('id'),
						'message'=>$this->model->getError()
				));
			}
		}
		$typeModel=new TypeModel();
		$tree=new Tree($typeModel->getAll());
		$this->view->setData('arctype',$tree->getAll());
		if(!empty($data[0]['flag'])){
			$data[0]['flag']=explode(',',$data[0]['flag']);
		}else{
			$data[0]['flag']=array();
		}
		$this->view->setData('article',$data[0]);
		$this->view->display();
	}
	public function delete(){
		if(!isAjaxRequest()){
			exit();
		}
		if(!empty($_POST)){
			if($this->model->updateIsDelete(post('id'))){
				exit('{"status":1,"message":"删除成功."}');
			}else{
				exit('{"status":0,"message":"删除失败."}');
			}
		}
	}
	public function showTrash(){
		$page=new Page($this->model->count(array('isdelete=?',array('isdelete'=>1))),array('url'=>'/News/admin.php/article/showTrash/{getIndex}/{pageNum}'));
		$this->view->setData('article',$this->model->selectByPage($page->getLimit(),1));
		$this->view->setData('page',$page->getHtml());
		$this->view->display();
	}
	public function revert(){
		if(!isAjaxRequest()){
			exit();
		}
		if(!empty($_POST)){
			if($this->model->updateIsDelete(post('id'),0)){
				exit('{"status":1,"message":"还原成功."}');
			}else{
				exit('{"status":0,"message":"还原失败."}');
			}
		}
	}
	public function trueDelete(){
		if(!isAjaxRequest()){
			exit();
		}
		if(!empty($_POST)){
			if($this->model->delete(post('id'))){
				exit('{"status":1,"message":"删除成功."}');
			}else{
				exit('{"status":0,"message":"删除失败."}');
			}
		}
	}
}